package cn.flying.cloud.oauth.web.oauth;

import javax.servlet.http.HttpServletRequest;

import java.util.Map;

import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 * 授权页面处理器
 *
 * @author: admin
 * @date: 2023年04月19日 16:27
 * @version: 1.0
 */
@Controller
@SessionAttributes("authorizationRequest")
public class OauthApprovalController {

    /**
     * 授权页面
     *
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/oauth/approval")
    public ModelAndView approval(Map<String, Object> model, HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        if (request.getAttribute("_csrf") != null) {
            view.addObject("_csrf", request.getAttribute("_csrf"));
        }
        view.setViewName("oauth-grant");
        AuthorizationRequest authorizationRequest = (AuthorizationRequest) model.get("authorizationRequest");
        view.addObject("clientId", authorizationRequest.getClientId());
        view.addObject("scope", authorizationRequest.getScope());
        return view;
    }

}
